package mathematical;

import java.util.Arrays;

public class CountPrimes {

    public int counterPrimes(int n) {
        boolean[] isPrime = new boolean[n];
        // 将数组都初始化为true
        Arrays.fill(isPrime, true);

        for (int i = 2; i * i < n; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j < n; j += i)
                    isPrime[j] = false;
            }
        }
        int count = 0;
        for (int i = 2; i < n; i++)
            if (isPrime[i])
                count++;

        return count;
    }
}
